from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .routers import labels, texts, annotations, export_import
from .db import init_db

app = FastAPI(title="TextLabel API")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(labels.router, prefix="/api")
app.include_router(texts.router, prefix="/api")
app.include_router(annotations.router, prefix="/api")
app.include_router(export_import.router, prefix="/api")

@app.get("/api/health")
def health():
    return {"code": 0, "message": "ok"}


@app.on_event("startup")
def on_startup():
    # 初始化数据库（创建表）
    init_db()
